<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link type="text/css" rel="stylesheet" href="/webjars/bootstrap/3.3.7/css/bootstrap.css">
    <script type="text/javascript" src="/webjars/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript" src="/webjars/bootstrap/3.3.7/js/bootstrap.js"></script>
    <script type="text/javascript" src="/webjars/vue/2.6.10/dist/vue.js"></script>
    <script type="text/javascript" src="/webjars/axios/0.19.0/dist/axios.js"></script>
</head>
<body>
<div id="vuebox">
    <div class="container" style="margin-top: 50px">
        <div class="row">
            <div class="col-md-6">
                <form class="form-inline" role="form" style="padding-left: 250px;">
                    <div class="form-group" style="padding-left: 20px;">
                        <label>图书分类:</label>
                        <select class="form-control" v-model="districtId">
                            <option value="0">全部</option>
                            <option v-for="item in districtList.data" :value="item.id">{{item.name}}</option>
                        </select>
                    </div>
                    <div class="form-group" style="padding-left: 20px;">
                        <button type="button" class="btn btn-primary" @click="selectAir(1)">查询</button>
                    </div>

                </form>
            </div>
            <div class="col-md-4 text-right">
                <a href="add_entry.html" class="btn btn-primary">新增电子图书</a>
            </div>
        </div>
        <br/>
        <table class="table table-striped">
            <thead>
            <tr>
                <th>图书编号</th>
                <th>图书名称</th>
                <th>图书摘要</th>
                <th>上传人</th>
                <th>上传时间</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="item in airQI.data.list">
                <td>{{item.id}}</td>
                <td>{{item.disname}}</td>
                <td>{{item.monitortime}}</td>
                <td>{{item.pm10}}</td>
                <td>{{item.pm25}}</td>
                <td>{{item.monitoringstation}}</td>
                <td>
                    <button class="btn btn-success" @click="to_update_entry(item.id)">修改</button>
                    <button class="btn btn-danger" @click="deleteEntry(item.id)">删除</button>
                </td>
            </tr>
            </tbody>
        </table>
        <br/>
        <div class="text-right">
            <ul class="pagination">
                <li v-show="airQI.data.hasPreviousPage">
                    <a href="#" @click.stop="selectAir(airQI.data.prePage)">&laquo;</a>
                </li>
                <li :class="{active:airQI.datapageNum==i}" v-for="i in airQI.data.navigatepageNums">
                    <a href="#" @click.stop="selectAir(i)">{{i}}</a>
                </li>
                <li v-show="airQI.data.hasNextPage">
                    <a href="#" @click.stop="selectAir(airQI.data.nextPage)">&raquo;</a>
                </li>
            </ul>
        </div>

        <!--模态框-->
        <div class="modal" id="mymodal">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button class="close" type="button" @click="hideModal()">&times;</button>
                        <div class="modal-title">
                            添加
                        </div>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal" role="form">
                            <input id="air_id" type="text" v-model="new_air.id">
                            <div class="form-group">
                                <label for="districtId" class="col-sm-2 control-label">区域</label>
                                <div class="col-sm-10">
                                    <select id="districtId" class="form-control" v-model="new_air.districtid">
                                        <option value="0">全部</option>
                                        <option v-for="item in DistrictList.data" :value="item.id">{{item.name}}</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="monitortime" class="col-sm-2 control-label">检测日期</label>
                                <div class="col-sm-10">
                                    <input v-model="new_air.monitortime" id="monitortime" type="text" class="form-control" placeholder="请输入日期">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="pm10" class="col-sm-2 control-label">PM10值</label>
                                <div class="col-sm-10">
                                    <input v-model="new_air.pm10" id="pm10" type="text" class="form-control" placeholder="请输入PM10值"/>
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="pm25" class="col-sm-2 control-label">PM2.5值</label>
                                <div class="col-sm-10">
                                    <input v-model="new_air.pm25" id="pm25" type="text" class="form-control" placeholder="请输入PM2.5值">
                                </div>
                            </div>
                            <div class="form-group">
                                <label for="monitoringstation" class="col-sm-2 control-label">监测站</label>
                                <div class="col-sm-10">
                                    <input v-model="new_air.monitoringstation" id="monitoringstation" type="text" class="form-control" placeholder="请输入监测站">
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-default" @click="hideModal()">关闭</button>
                        <button class="btn btn-primary" @click="saveEntry()">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>
<script type="text/javascript">
    var vm = new Vue({
        el: "#vuebox",
        data: {
            districtList: {},
            districtId: "0",
            airQI: {},
            pageNum: '',
            delResult:{},
            new_entry:{id:'',districtid:'0',monitortime:'',pm10:'',pm25:'',monitoringstation:''}
        },
        methods: {
            selectAir: function (pageNum) {
                axios.get("/selectAir", {params: {districtId: this.districtId, pageNum: pageNum}})
                    .then(res => this.airQI = res.data);
            },
            deleteEntry:function (id) {
                axios.get("/delEntryId",{params:{id:id}}).then(res=>{
                    this.delResult=res.data;
                    if(this.delResult.result=='SUCCESS'){
                        alert("删除成功")
                        this.selectAir(this.pageNum);
                    }else{
                        alert(this.delResult.message);
                    }
                });
            },
            showModal:function () {
                $('#mymodal').modal('show');
            },
            hideModal:function () {
                $('#mymodal').modal('hide');
                this.clearForm();
            },
            clearForm:function () {
                $('#entry_id').val('');
                $('#districtid').val(0);
                $('#monitortime').val('');
                $('#pm10').val('');
                $('#pm25').val('');
                $('#monitoringstation').val('');
                // $('#lastmodifytime').val('');
            },
            saveEntry:function () {
                axios.post("/saveEntry",this.new_entry).then(res=>{
                    this.delResult=res.data;
                    if(this.delResult.result=='SUCCESS'){
                        this.selectAir(this.pageNum);
                        this.hideModal();
                    }else{
                        alert(this.delResult.message);
                    }
                })
            },
            to_update_entry(id){
                axios.get("/getEntryId",{params:{id:id}}).then(res=>{
                    this.new_entry=res.data.data;
                    this.showModal();
                });
            }
        },
        mounted() {
            axios.get("/getDistrictList").then(res => this.districtList = res.data);
            this.selectAir(1);
        }
    })
</script>